/**************************************************************************
*  Black Knight - Chess Engine                                            *
*  Copyright (C) 2009  Victor Rusu                                        *
*                                                                         *
*  This program is free software: you can redistribute it and/or modify   *
*  it under the terms of the GNU General Public License as published by   *
*  the Free Software Foundation, either version 3 of the License, or      *
*  (at your option) any later version.                                    *
*                                                                         *
*  This program is distributed in the hope that it will be useful,        *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           *
*  GNU General Public License for more details.                           *
*                                                                         *
*  You should have received a copy of the GNU General Public License      *
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
**************************************************************************/

#include "io/xboard.h"

#include <string>
#include <sstream>
#include "io/write.h"
#include "logic/engine.h"

Xboard* Xboard::xboard_instance_ = NULL;
boost::mutex Xboard::instance_lock_;

Xboard::Xboard() : protocol_version(1) {
}

Xboard::~Xboard() {
}

Xboard* Xboard::instance() {
    instance_lock_.lock();
    if (xboard_instance_ == NULL) {
        xboard_instance_ = new Xboard;
    }
    instance_lock_.unlock();
    return xboard_instance_;
}

void Xboard::receive_command(std::string command) {
    std::istringstream command_line(command);

    std::string command_name;
    command_line >> command_name;

    if (command_name == "xboard") {
    } else if (command_name == "protover") {
        command_line >> protocol_version;
        if (protocol_version > 1) {
            std::ostringstream answer;
            answer << "feature";
            answer << " ping=0";
            answer << " setboard=1";
            answer << " playother=0";
            answer << " draw=0";
            answer << " sigint=0";
            answer << " sigterm=0";
            answer << " analyze=0";
            answer << " variants=\"normal\"";
            answer << " colors=1";
            answer << " myname=\"" << Engine::instance()->kEngineName
                    << " " << Engine::instance()->kEngineVersion << "\"";
            answer << " done=1";
            send_command(answer.str());
        }
    } else if (command_name == "quit") {
        exit(0);
    } else {
        command_queue_.push(command);
    }
}

void Xboard::send_command(std::string command) {
    Write::instance()->give_command(command);
}

std::string Xboard::get_command() {
    std::string command = "";
    if (!command_queue_.empty()) {
        command = command_queue_.front();
        command_queue_.pop();
    }
    return command;
}
